//拓展JS自带的Date,增加格式化 fmt="yyyy-MM-dd HH:mm:ss 星期E"
Date.prototype.format = function (fmt) {
  var o = {
    'M+': this.getMonth () + 1, //月份
    'd+': this.getDate (), //日
    'h+': this.getHours () % 12 == 0 ? 12 : this.getHours () % 12, //12小时制
    'H+': this.getHours (), //24小时制
    'm+': this.getMinutes (), //分
    's+': this.getSeconds (), //秒
    'q+': Math.floor ((this.getMonth () + 3) / 3), //季度
    S: this.getMilliseconds (), //毫秒
  };
  var week = {
    0: '\u65e5',
    1: '\u4e00',
    2: '\u4e8c',
    3: '\u4e09',
    4: '\u56db',
    5: '\u4e94',
    6: '\u516d',
  };
  if (/(y+)/.test (fmt)) {
    fmt = fmt.replace (
      RegExp.$1,
      (this.getFullYear () + '').substr (4 - RegExp.$1.length)
    );
  }
  if (/(E+)/.test (fmt)) {
    fmt = fmt.replace (
      RegExp.$1,
      (RegExp.$1.length > 1
        ? RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468'
        : '') + week[this.getDay () + '']
    );
  }
  for (var k in o) {
    if (new RegExp ('(' + k + ')').test (fmt)) {
      fmt = fmt.replace (
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr (('' + o[k]).length)
      );
    }
  }
  return fmt;
};

Date.prototype.getCurrentMonthFirst = function (fmt) {
  // 获取当前月的第一天

  var date = new Date ();
  date.setDate (1);
  return date;
};
Date.prototype.getCurrentMonthLast = function (fmt) {
  // 获取当前月的最后一天
  var date = new Date ();
  var currentMonth = date.getMonth ();
  var nextMonth = ++currentMonth;
  var nextMonthFirstDay = new Date (date.getFullYear (), nextMonth, 1);
  var oneDay = 1000 * 60 * 60 * 24;
  return new Date (nextMonthFirstDay - oneDay);
};
Date.prototype.getCurrentWeekFirst = function (fmt) {
  // 获取这周的周一
  var date = new Date ();

  var weekday = date.getDay () || 7; //获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7
  date.setDate (date.getDate () - weekday + 1); //往前算（weekday-1）天，年份、月份会自动变化

  return date;
};

var minDate = new Date ('1900/01/01');
var showDateHour = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  dateStr = dateStr.replace ('T', ' ');
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('yyyy-MM-dd HH:mm')
    : '';
};

// 格式化new date出来的时间
// Wed Oct 27 2021 08:19:00 GMT+0800 (中国标准时间)
var showDateHour2 = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('yyyy-MM-dd HH:mm')
    : '';
};

var showDate = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('yyyy-MM-dd')
    : '';
};

var showFullDate = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  dateStr = dateStr.replace ('T', ' ');
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('yyyy-MM-dd HH:mm:ss')
    : '';
};
var showDateTime = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('yyyy-MM-dd HH:mm:ss')
    : '';
};
var showHourMinSec = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  //dateStr=dateStr.replace('T',' ');
  return dateStr && new Date (dateStr).getTime () > minDate.getTime ()
    ? new Date (dateStr).format ('HH:mm:ss')
    : '';
};

var showSpacingTime = function (dateStr1, dateStr2) {
  if (
    !dateStr1 ||
    new Date (dateStr1).getTime () < minDate.getTime () ||
    !dateStr2 ||
    new Date (dateStr2).getTime () < minDate.getTime ()
  ) {
    return '';
  }

  var stime = new Date (dateStr1).getTime ();
  var etime = new Date (dateStr2).getTime ();
  var usedTime = Math.abs (etime - stime) / 1000;
  var days = Math.floor (usedTime / (24 * 3600));
  var leave1 = usedTime % (24 * 3600);
  var hours = Math.floor (leave1 / 3600);
  var leave2 = leave1 % 3600;
  var minutes = Math.floor (leave2 / 60);
  var time = days + '天' + hours + '时' + minutes + '分';

  return time;
};

var showPreMonthDate = function (dateStr) {
  if (!dateStr) {
    return '';
  }
  let times = new Date (dateStr);
  let Y = times.getFullYear ();
  let M = times.getMonth () + 1;
  let D = times.getDate ();
  let H = times.getHours () > 10 ? times.getHours () : '0' + times.getHours ();
  let m = times.getMinutes () > 10
    ? times.getMinutes ()
    : '0' + times.getMinutes ();
  let s = times.getSeconds () > 10
    ? times.getSeconds ()
    : '0' + times.getSeconds ();
  let MD = Y + '-';
  if (D - 30 > 0) {
    MD += M + '-' + (D - 30);
  } else {
    M = times.getMonth ();
    if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) {
      MD += M + '-' + (D - 30 + 31);
    } else if (M == 4 || M == 6 || M == 9 || M == 11) {
      MD += M + '-' + (D - 30 + 30);
    } else {
      if ((Y % 4 == 0 && Y % 100 != 0) || Y % 400 == 0) {
        //闰年
        MD += M + '-' + (D - 30 + 29);
      } else {
        MD += M + '-' + (D - 30 + 28);
      }
    }
  }
  return MD + ' ' + H + ':' + m + ':' + s;
};
//获取根据传入天数，获取时间
var getDay = function (day) {
  var today = new Date ();
  var targetday_milliseconds = today.getTime () + 1000 * 60 * 60 * 24 * day;
  today.setTime (targetday_milliseconds); //注意，这行是关键代码
  return showDate (today);
};
export default {
  showDateHour: showDateHour,
  showDate: showDate,
  showFullDate: showFullDate,
  showDateTime: showDateTime,
  showHourMinSec: showHourMinSec,
  showSpacingTime: showSpacingTime,
  showPreMonthDate: showPreMonthDate,
  showDateHour2: showDateHour2,
  getDay: getDay,
};
